package com.lanou.renrenxin.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.renrenxin.R;
import com.lanou.renrenxin.dao.ChatList;
import com.lanou.renrenxin.onclicklistener.OnChatListItemClickListener;
import com.lanou.renrenxin.singleton.RenrenxinSingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 16/3/2.
 * 聊天主界面适配器
 */
public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ChatListViewHolder> {
    private Context context;
    private List<ChatList> chatBeanList;
    private OnChatListItemClickListener onChatListItemClickListener;
    private String toUserName;
    private String mUsername;


    public void setOnChatListItemClickListener(OnChatListItemClickListener onChatListItemClickListener) {
        this.onChatListItemClickListener = onChatListItemClickListener;
    }

    public ChatListAdapter(Context context, String toUserName, String mUsername) {
        this.context = context;
        this.toUserName = toUserName;
        this.mUsername = mUsername;
        initData();
    }

    private void initData() {
        chatBeanList = new ArrayList<>();
        chatBeanList = RenrenxinSingleton.getInstence().queryChatList(mUsername);
    }

    @Override
    public ChatListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_listchat, parent, false);
        ChatListViewHolder viewHolder = new ChatListViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ChatListViewHolder holder, int position) {
        holder.pos = position;
        holder.userPhoto.setImageResource(R.mipmap.headimg);
        holder.userName.setText(chatBeanList.get(position).getUsername());
        holder.recentChat.setText(chatBeanList.get(position).getBody());
        holder.time.setText(chatBeanList.get(position).getTime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChatListItemClickListener.onItemClick(chatBeanList.get(holder.pos).getUsername());
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == chatBeanList ? 0 : chatBeanList.size();
    }

    public void refreshData() {
        chatBeanList.clear();
        chatBeanList = RenrenxinSingleton.getInstence().queryChatList(mUsername);
        notifyDataSetChanged();
    }

    class ChatListViewHolder extends RecyclerView.ViewHolder {
        private ImageView userPhoto;
        private TextView userName, recentChat, time;
        private int pos;

        public ChatListViewHolder(View itemView) {
            super(itemView);
            userPhoto = (ImageView) itemView.findViewById(R.id.iv_userphoto);
            userName = (TextView) itemView.findViewById(R.id.tv_username);
            recentChat = (TextView) itemView.findViewById(R.id.tv_recentchat);
            time = (TextView) itemView.findViewById(R.id.tv_recenttime);
            pos = 0;
        }
    }

}
